package com.chixing.dao.impl;

import com.chixing.dao.RowMapper;
import com.chixing.entity.Employee;
import com.chixing.util.DBConnection;

import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import java.util.function.Function;

public class JDBCTemplate<E> {
    private RowMapper<E> rowMapper;
    public JDBCTemplate(RowMapper<E> rowMapper) {
        this.rowMapper = rowMapper;
    }
    public JDBCTemplate() {
    }

    public void setRowMapper(RowMapper<E> rowMapper) {
        this.rowMapper = rowMapper;
    }

    /**
     * 支持数据的增删改操作
     * @param sql
     * @param args
     * @return 返回表中受影响的行数
     */
    public int update(String sql, Object... args) {
        Connection conn = DBConnection.getConnection();
        PreparedStatement ps = null;
        int count = 0;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            count = ps.executeUpdate();
            System.out.println("操作成功");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    // 查询单个记录
    public E selectForObject(String sql, Object... args) {
        Connection conn = DBConnection.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        E ea = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++){
                ps.setObject(i + 1, args[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                ea =rowMapper.mapper(rs);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (ps != null) ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DBConnection.close(conn);
        }
        return ea;
    }

    // 查询多个记录
    public List<E> selectForList(String sql,Object... args) {
        Connection conn = DBConnection.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<E> result = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++){
                ps.setObject(i + 1, args[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                E ea = rowMapper.mapper(rs);
                result.add(ea);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (ps != null) ps.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            DBConnection.close(conn);
        }
        return result;
    }
}
